/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.servicesManagement.model;

import com.serviceManagement.constants.LineOfBusinessType;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

import org.hibernate.Criteria;
import org.hibernate.Query;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Expression;

/**
 *
 * @author Aruna
 */
public class Client {

    public static void main(String[] args) {


        SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = sf.openSession();
        Transaction t = session.beginTransaction();

        LineOfBusiness lob = new LineOfBusiness();
        lob.setChangedByUserId(BigDecimal.ZERO);
        lob.setChangedDate(new Date());
        lob.setCreatedByUserId(BigDecimal.ZERO);
        lob.setCreatedDate(new Date());
        lob.setDescription("CREDIT_1");
        //lob.setLobId(new Long("167"));




        ServiceLine sl = new ServiceLine();
        sl.setChangedByUserId(BigDecimal.ZERO);
        sl.setChangedDate(new Date());
        sl.setCreatedByUserId(BigDecimal.ZERO);
        sl.setCreatedDate(new Date());
        sl.setName("360 Control");
        sl.setDescription("360 Control Description");



        lob.getServiceLine().add(sl);







        //session.save(lob);


        Service service = new Service();
        service.setServiceLine(new ServiceLine(new Long("171")));
        service.setDescription("360 Control Service");
        service.setChangedByUserId(BigDecimal.ZERO);
        service.setChangedDate(new Date());
        service.setCreatedByUserId(BigDecimal.ZERO);
        service.setCreatedDate(new Date());



        Attribute attribute = new Attribute();
        attribute.setDescription("360 Attribute 1");
        attribute.setChangedByUserId(BigDecimal.ZERO);
        attribute.setChangedDate(new Date());
        attribute.setCreatedByUserId(BigDecimal.ZERO);
        attribute.setCreatedDate(new Date());


        attribute.setService(service);




        //  session.save(attribute);

        Aspect aspect = new Aspect();
        aspect.setName("Country");
        aspect.setDescription("Country Name");
        aspect.setChangedByUserId(BigDecimal.ZERO);
        aspect.setChangedDate(new Date());
        aspect.setCreatedByUserId(BigDecimal.ZERO);
        aspect.setCreatedDate(new Date());

        AspectConf aspectConf = new AspectConf();

        aspectConf.setAttribute(attribute);
        aspectConf.setChangedByUserId(BigDecimal.ZERO);
        aspectConf.setChangedDate(new Date());
        aspectConf.setCreatedByUserId(BigDecimal.ZERO);
        aspectConf.setCreatedDate(new Date());
        aspectConf.setAspect(aspect);

        aspect.getAspectConfs().add(aspectConf);

        // session.save(aspect);

        t.commit();


        session = sf.openSession();
        session.beginTransaction();
        Query query = session.createQuery("from Service where createdDate= :createdDate");
        query.setParameter("createdDate", new Date());



        for (Service item : (List<Service>) query.list()) {

            System.out.println("item.getService()::" + item.getAttribute());

        }

        session.getTransaction().commit();
        session.close();


        //createService(sf, LineOfBusinessType.CREDIT, "Remote Deposit Capture","RDC-Remote Deposit Capture","171");


        //addAttribute(sf, new Long("228"),new Long("242"), "Attribute Description 2");

        // addAspect(sf, new Long("242"), null, "Country", "Country");

        System.out.println("Test");

        // deleteAspect(sf, new Long("257"));

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);

        try {
            // System.out.println(mapper.writeValueAsString(getService(sf, new Long("228"))));

            System.out.println(
                    mapper.readValue(new File("e:\\user.json"), Service.class));

            System.out.println(mapper.writeValueAsString(mapper.readValue(new File("e:\\user.json"), Service.class)));

        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    public void createNewService(Service service) {

        SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = sf.openSession();
        createService(sf, service);
    }

    private static void createService(SessionFactory sessionFactory, Service service) {

        Session session = sessionFactory.openSession();
        save(session, service);

        session.close();


    }

    private static void createService(SessionFactory sessionFactory, LineOfBusinessType lineOfBusinessType, String name, String description, String serviceLineId) {

        Session session = sessionFactory.openSession();

        Criteria criteria = session.createCriteria(LineOfBusiness.class);
        criteria.add(Expression.eq("description", LineOfBusinessType.CREDIT.toString()));

        LineOfBusiness lob = (LineOfBusiness) criteria.uniqueResult();


        ServiceLine sl = new ServiceLine();
        sl.setChangedByUserId(BigDecimal.ZERO);
        sl.setChangedDate(new Date());
        sl.setCreatedByUserId(BigDecimal.ZERO);
        sl.setCreatedDate(new Date());
        sl.setName(name);
        sl.setDescription(description);



        lob.getServiceLine().add(sl);


        Service service = new Service();
        service.setServiceLine(new ServiceLine(new Long(serviceLineId)));
        service.setDescription(name);
        service.setChangedByUserId(BigDecimal.ZERO);
        service.setChangedDate(new Date());
        service.setCreatedByUserId(BigDecimal.ZERO);
        service.setCreatedDate(new Date());


        update(session, lob);
        save(session, service);

        session.close();

    }

    private static void addAttribute(SessionFactory sessionFactory, Long serviceId, Long attributeId, String description) {

        Session session = sessionFactory.openSession();

        Service service = (Service) session.load(Service.class, serviceId);

        Attribute attribute = new Attribute();
        attribute.setDescription(description);
        attribute.setChangedByUserId(BigDecimal.ZERO);
        attribute.setChangedDate(new Date());
        attribute.setCreatedByUserId(BigDecimal.ZERO);
        attribute.setCreatedDate(new Date());

        attribute.setService(service);

        if (attributeId != null) {
            attribute.setAttributeId(attributeId);
            update(session, attribute);

        } else {
            save(session, attribute);

        }


        session.close();

    }

    private static void addAspect(SessionFactory sessionFactory, Long attributeId, Long aspectId, String description, String name) {

        Session session = sessionFactory.openSession();

        Attribute attribute = (Attribute) session.load(Attribute.class, attributeId);

        Aspect aspect = new Aspect();
        aspect.setName(name);
        aspect.setDescription(description);
        aspect.setChangedByUserId(BigDecimal.ZERO);
        aspect.setChangedDate(new Date());
        aspect.setCreatedByUserId(BigDecimal.ZERO);
        aspect.setCreatedDate(new Date());

        AspectConf aspectConf = new AspectConf();

        aspectConf.setAttribute(attribute);
        aspectConf.setChangedByUserId(BigDecimal.ZERO);
        aspectConf.setChangedDate(new Date());
        aspectConf.setCreatedByUserId(BigDecimal.ZERO);
        aspectConf.setCreatedDate(new Date());
        aspectConf.setAspect(aspect);

        aspect.getAspectConfs().add(aspectConf);

        // session.save(aspect);

        if (aspectId != null) {
            aspect.setAspectId(aspectId);
            update(session, aspect);

        } else {
            save(session, aspect);

        }


        session.close();

    }

    private static void save(Session session, Object o) {


        try {
            session.beginTransaction();
            session.save(o);
            session.getTransaction().commit();
        } finally {
        }

    }

    private static void update(Session session, Object o) {


        try {
            session.beginTransaction();
            session.update(o);
            session.getTransaction().commit();
        } finally {
        }

    }

    private static Service getService(SessionFactory sessionFactory, Long serviceId) {

        Session session = sessionFactory.openSession();
        return (Service) session.load(Service.class, serviceId);


    }

    private static void deleteAspect(SessionFactory sessionFactory, Long aspectId) {

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        /* AspectConf aspectConf = new AspectConf(aspectId);
         aspectConf.setAspect(null);
         session.delete(aspectConf);*/

        Criteria c = session.createCriteria(Aspect.class).add(Expression.eq("aspectId", aspectId));
        Aspect aspect = (Aspect) c.uniqueResult();

        for (AspectConf confItem : aspect.getAspectConfs()) {
            session.delete(confItem);
        }

        session.delete(aspect);

        session.getTransaction().commit();
        session.close();


    }
}
